package yur.wedding.modules.sys.service.impl;

import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import org.springframework.transaction.annotation.Transactional;
import yur.wedding.common.utils.Query;

import yur.wedding.modules.sys.dao.SysRoleDeptDao;
import yur.wedding.modules.sys.entity.SysRoleDeptEntity;
import yur.wedding.modules.sys.service.SysRoleDeptService;
import yur.wedding.utils.PageUtils;

/**
 * 角色与菜单对应关系
 */

@Service("sysRoleDeptService")
public class SysRoleDeptServiceImpl extends ServiceImpl<SysRoleDeptDao, SysRoleDeptEntity> implements SysRoleDeptService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<SysRoleDeptEntity> page = this.page(
                new Query<SysRoleDeptEntity>().getPage(params),
                new QueryWrapper<SysRoleDeptEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public List<Long> queryDeptIdList(Long[] roleId) {

        List<Long> deptId = baseMapper.queryDeptIdList(roleId);

        return deptId;
    }


    /**
     * 添加角色与部门的 关系 以及修改操作
     * @param roleId
     * @param deptIdList
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveOrUpdate(Long roleId, List<Long> deptIdList) {
        //先删除角色与部门关联权限
        deleteBatch(new Long[]{roleId});
        if (deptIdList.size() == 0) {
            return;
        }
        for (Long deptlist : deptIdList) {

            SysRoleDeptEntity roleDeptEntity = new SysRoleDeptEntity();
            roleDeptEntity.setDeptId(deptlist);
            roleDeptEntity.setRoleId(roleId);

            this.save(roleDeptEntity);
            System.out.println("添加角色与部门关联权限 成功");
        }
    }

    private void deleteBatch(Long[] longs) {

        baseMapper.deleteBatch(longs);
    }

}
